package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Role;
import com.qk.management.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Slf4j
@RestController
@RequestMapping("/roles")
public class RoleController {

    @Autowired
    private RoleService roleService;


    @GetMapping("/list")
    public Result getAll(){
        return Result.success(roleService.getAll());
    }


    @PutMapping
    public Result updateRole(@RequestBody Role role){
        System.out.println(role.getId());
        roleService.updateRole(role);
        return Result.success();
    }


    @GetMapping("/{id}")
    public Result getRoleById(@PathVariable Integer id){
        return Result.success(roleService.getRoleById(id));
    }


    @PostMapping
    public Result addRole(@RequestBody  Role role){
        roleService.addRole(role);
        return Result.success();
    }


    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable("id") Integer id){
        System.out.println(id);
        roleService.deleteById(id);
        return Result.success();
    }


    @GetMapping
    public Result getAllRoles(
            @RequestParam(name = "name",required = false) String name,
            @RequestParam(name = "label",required = false) String label,
            @RequestParam(name = "page", defaultValue = "1") Integer page,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize
    ) {
        System.out.println(page);
        PageResult<Role> roles = roleService.getAllRoles(name, label, page, pageSize);
        return Result.success(roles);
    }
}
